package com.example.universita.esami;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

import com.example.universita.esami.ProceduraGuidata.SetDataUniversity;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.support.v4.app.NavUtils;

public class Dialog_docenti extends Activity {

	ArrayAdapter<String> arrayAdapter;
	DBDataUniversity db;
	int nameCol;
	boolean orderNome = true;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dialog_docenti);
        
        arrayAdapter = new ArrayAdapter<String>(this, R.layout.listitem, R.id.listItemTextView);
        db = new DBDataUniversity(this);
		
        ListView listView = (ListView) findViewById(R.id.Lista_docenti);
        
		listView.setAdapter(arrayAdapter);
		
		listView.setOnItemClickListener(new OnItemClickListener() {
		
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
				
				ListView listView = (ListView) adapterView;
				ArrayAdapter<String> arrayAdapter = (ArrayAdapter<String>) listView.getAdapter();
				String item = arrayAdapter.getItem(position);
				Intent intent = new Intent (getApplicationContext(), Docenti.class);
				String pkg = getPackageName();
				intent.putExtra(pkg+".Docente", item);
				startActivity(intent);
				
			}
		});
    
		aggiornaElenco();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_dialog_docenti, menu);
        return true;
    }
    
    @Override
    public void onResume() {
    	aggiornaElenco();
    	super.onResume();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	Intent intent;
    	String pkg;
    	Cursor cursor;
    	ListView listView;
    	
    	switch(item.getItemId()){
    	    case R.id.nuovo: // Salva
    	    		intent = new Intent (getBaseContext(), EditDocenti.class);
    	    		pkg = getPackageName();
    	    		intent.putExtra(pkg+".daDove", false);
    	    		startActivity(intent);
    	    	    break;     
    	    case R.id.statistiche: // Vai a Dettagli Esame
 	    	   		intent = new Intent (this, StatisticActivity.class);
 	    	   		startActivity(intent);
 	    	   		break;      
    	    case R.id.carriera: // Vai a Carriera
 	    	   		intent = new Intent(this, Carriera.class);
 	    	   		startActivity(intent);  
 	    	   		break;      
    	    case R.id.dettagli: // Vai a Carriera
	    	   		intent = new Intent(this, Details.class);
	    	   		startActivity(intent);  
	    	   		break;         		
    	    case R.id.home: // Vai alla Home
   	   	   			intent = new Intent(this, MainActivity.class);
   	   	   			startActivity(intent);  
   	   	   			break;  
    	    case R.id.nome:
    	    		db.open();
    	    		arrayAdapter.clear();
    	    		orderNome = !(orderNome);
    	    		if(orderNome)  cursor = db.getProfOrder("name ASC");
    	    		else   		   cursor = db.getProfOrder("name DESC");
    			
    	    		while(cursor.moveToNext()){
    	    			nameCol = cursor.getColumnIndex("name");
    	    			arrayAdapter.add(cursor.getString(nameCol));
    	    		}
    			
    	    		db.close();
    	    		
    	    		listView = (ListView) findViewById(R.id.Lista_docenti);
    	    		arrayAdapter.notifyDataSetChanged();
    	    		listView.setAdapter(arrayAdapter);  
    	    	 break;
    	}
     return true;
    }
    
    public void aggiornaElenco(){
    	db.open();
    	arrayAdapter.clear();
		Cursor cursor = db.getProfOrder("name ASC");
		
		while(cursor.moveToNext()){
			nameCol = cursor.getColumnIndex("name");
			arrayAdapter.add(cursor.getString(nameCol));
			
		}
		arrayAdapter.notifyDataSetChanged();
		db.close();
    }
}




